package com.war.service.warehousing;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.war.dao.warehousing.PurchaseDao;
import com.war.pojo.stock.TransferInfo;
import com.war.pojo.system.Supplier;
import com.war.pojo.system.User;
import com.war.pojo.system.Warehouse;
import com.war.pojo.warehousing.PurchaseInfo;
import com.war.pojo.warehousing.ReturnInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Console;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class PurchaseServiceImpl implements PurchaseService{

    @Autowired(required = false)
    PurchaseDao purchaseDao;

    @Override
    public HashMap<String, Object> selectAll(PurchaseInfo purchaseInfo) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //设置分页参数
        PageHelper.startPage(purchaseInfo.getPage(),purchaseInfo.getRow());
        List<PurchaseInfo> purchaseList = new ArrayList<>();

        //查询数据库表数据
        //根据用户选择的查询条件进行查询
        //判断用户输入的查询条件是否有值
        if(purchaseInfo.getConValue().equals("") && !purchaseInfo.getCondition().equals("入库日期范围查询")){
            purchaseList = purchaseDao.selectAll();
        }else{
            if(purchaseInfo.getCondition().equals("产品名")){
                purchaseList = purchaseDao.selectByProductName(purchaseInfo);
            }else if(purchaseInfo.getCondition().equals("供应商")){
                purchaseList=purchaseDao.selectBySupplierName(purchaseInfo);
            }else if(purchaseInfo.getCondition().equals("采购员")) {
                purchaseList=purchaseDao.selectByBuyer(purchaseInfo);
            }else if(purchaseInfo.getCondition().equals("入库日期范围查询")) {
                purchaseList=purchaseDao.selectByDate(purchaseInfo);
            }else if(purchaseInfo.getCondition().equals("仓库名称")) {
                purchaseList=purchaseDao.selectByStorageName(purchaseInfo);
            }else if(purchaseInfo.getCondition().equals("仓库管理员")) {
                purchaseList=purchaseDao.selectByStorageManager(purchaseInfo);
            }else{
                purchaseList=purchaseDao.selectAll();
            }
        }

        //把查询的数据转换成分页对象
        PageInfo<PurchaseInfo> page = new PageInfo<PurchaseInfo>(purchaseList);

        //获取分页的当前页的集合
        map.put("purchaseList",page.getList());
        //总条数
        map.put("total",page.getTotal());
        //总页码
        map.put("totalPage",page.getPages());
        //上一页
        //判断上一页的值是否等于第一页
        if(page.getPrePage() == 0)
        {
            map.put("pre",1);
        }else{
            map.put("pre",page.getPrePage());
        }
        //下一页
        //保持在最后一页
        if(page.getNextPage()==0){
            map.put("next",page.getPages());
        }else{
            map.put("next",page.getNextPage());
        }
        //当前页
        map.put("cur",page.getPageNum());

        return map;
    }

    @Override
    public PurchaseInfo selectById(PurchaseInfo purchaseInfo) {
        return purchaseDao.selectById(purchaseInfo);
    }

    //供应商
    @Override
    public List<Supplier> selectSupplierName() {
        return purchaseDao.selectSupplierName();
    }

    @Override
    public String getSupplierTel(Supplier supplier) {
        return purchaseDao.getSupplierTel(supplier);
    }

    //仓库名称
    @Override
    public List<Warehouse> selectStorageName() {
        return purchaseDao.selectStorageName();
    }

    @Override
    public String getStorageManager(Warehouse warehouse) {
        return purchaseDao.getStorageManager(warehouse);
    }

    //采购员
    @Override
    public List<User> selectBuyerName() {
        return purchaseDao.selectBuyerName();
    }

    @Override
    public String editPurchase(PurchaseInfo purchaseInfo) {
        int num = purchaseDao.editPurchase(purchaseInfo);
        if(num > 0){
            return "修改成功";
        }
        return "修改失败";
    }

    @Override
    public String delPurchase(PurchaseInfo purchaseInfo) {
        int num = purchaseDao.delPurchase(purchaseInfo);
        if(num > 0){
            return "删除成功";
        }
        return "删除失败";
    }

    @Override
    public String insertPurchase(PurchaseInfo purchaseInfo) {
        int num = purchaseDao.insertPurchase(purchaseInfo);
        if(num > 0){
            return "添加成功";
        }
        return "添加失败";
    }

    @Override
    public String purchaseReturn(ReturnInfo returnInfo) {
        int num = purchaseDao.purchaseReturn(returnInfo);
        if(num > 0){
            return "退货成功";
        }
        return "退货失败";
    }

    @Override
    public int storageNumNow(PurchaseInfo purchaseInfo) {
        return purchaseDao.storageNumNow(purchaseInfo);
    }

    @Override
    public String delAll(List<PurchaseInfo> purchaseInfo) {
        int num = 0;
        for(PurchaseInfo p:purchaseInfo){
            num = purchaseDao.delAll(p);
            num++;
        }
        if(num == purchaseInfo.size()){
            return "删除成功";
        }
        return "删除失败";
    }

}
